<?php 
/**
 * @Author: YeMiao
 * @Date:   2017-08-12 15:33:46
 * @Last Modified by:   YeMiao
 * @Last Modified time: 2017-10-23 16:23:53
 */
namespace app\common\model;
use app\common\model\AdminLog;
use app\common\model\AdminGroup;
use think\Model;

class Article extends model{
	protected $resultSetType = 'collection';

	// 获取全部数据
	public function get_list(){		
		$article = $this
			->alias('a')
			->field('a.*,b.catename')
			->join('category b','a.category = b.id')
			->select()
			->toArray();
		return $article;
	}
 // 获取某栏目的数据 
	public function find_cate_list($id){
		$article = $this
			->alias('a')
			->field('a.*,b.catename')
			->join('category b','a.category = b.id')
			->where('a.category',$id)
			->order('a.create_time desc')
			->select()
			->toArray();
		return $article;
	}

	// 添加数据
	public function add_article($data){		

		$data['user'] = input('session.user');
		try {
			$article = $this->allowField(true)->save($data);		
		} catch (\Exception $e) {
			return 0;
		}
		$log = new AdminLog();
		$log->add_log('添加文章');	
		return $article;
	}

	public function del_article($data){
		$find = $this->get($data['id']);
		$log = new AdminLog();
		$log->add_log('删除文章:'.$find['title']);
		try {
			$article = $this->destroy($data['id']);			
		} catch (\Exception $e) {
			return 0;
		}	
		return $article;
	}

	public function update_article($data){
		$log = new AdminLog();
		$log->add_log('修改文章:');
		$article = $this->update($data);
		return $article;
	}
}

?>